
package action;

import beans.ForumBean;
import beans.MessageBean;
import beans.UserBean;
import db.Forum;
import db.Message;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;

/**
 * Show edit forum for message editing.
 * @author Tomas CASTA
 */
@UrlBinding("/showEdit.action")
public class ShowEditActionBean implements ActionBean {
    /** action context  */
    private ActionBeanContext context;
    /* message bean */
    private MessageBean messageBean;
    /** message text */
    @Validate(required = true)
    private String text;
    /** message id to edit */
    private String idEdit;
    /** forum id for show */
    private String idShow;
    /** error message */
    private String errMsg;
    /** forum bean */
    private ForumBean forumBean;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public void setMessageBean(MessageBean messageBean) {
        this.messageBean = messageBean;
    }

    public MessageBean getMessageBean() {
        return messageBean;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getIdEdit() {
        return idEdit;
    }

    public String getIdShow() {
        return idShow;
    }

    public void setIdEdit(String idEdit) {
        this.idEdit = idEdit;
    }

    public void setIdShow(String idShow) {
        this.idShow = idShow;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public ForumBean getForumBean() {
        return forumBean;
    }

    public void setForumBean(ForumBean forumBean) {
        this.forumBean = forumBean;
    }


    /**
     * Show form for editing message.
     * @return Resolution
     */
    @DefaultHandler
    @DontValidate
    public Resolution edit() {

        /* User is logged */
        if (null != context.getRequest().getSession().getAttribute("user")) {

            /* Get user info from session */
            UserBean user = (UserBean) context.getRequest().getSession().getAttribute("user");

            /* User is admin */
            if (user.getAdmin() == 1) {
                Message message = new Message();
                message.setDb(this.context.getServletContext());
                Forum forum = new Forum();
                forum.setDb(this.context.getServletContext());
                try {
                    /* Parse id message to edit */
                    int idEditValue = Integer.valueOf(context.getRequest().getParameter("idEdit"));

                    /* Parse forum id */
                    int idShowValue = Integer.valueOf(context.getRequest().getParameter("idShow"));
                    
                    /* Edit message */
                    message.showMessage(idEditValue);

                    /* Set new message bean */
                    messageBean = message.getMessage();
                    this.setText(message.getMessage().getText());

                    /* Set forum bean */
                    forum.showForumById(idShowValue);
                    forumBean = forum.getForum();
                    
                    /* Return resolution with new bean */
                    return new RedirectResolution("/jsp/editMessage.jsp").flash(this).includeRequestParameters(true);
                } catch (NumberFormatException e) {
                    Logger.getLogger(DeleteMessageActionBean.class.getName()).log(Level.SEVERE, null, e);
                    return new ForwardResolution("/index.action");
                }
            }
        }
        /* Some error, show index */
        return new ForwardResolution("/index.action");
    }

    /**
     * Prepare for new resolution.
     */
    @Before(stages = LifecycleStage.ResolutionExecution)
    public void prepare() {
        try {
            Message message = new Message();
            message.setDb(this.context.getServletContext());
            Forum forum = new Forum();
            forum.setDb(this.context.getServletContext());

            /* Get message id to edit from form */
            int idEditValue = Integer.valueOf(idEdit);
            message.showMessage(idEditValue);

            /* Parse forum id */
            int idShowValue = Integer.valueOf(idShow);

            /* Set forum bean */
            forum.showForumById(idShowValue);
            forumBean = forum.getForum();

            /* Set text to bean */
            messageBean = message.getMessage();
            this.setText(messageBean.getText());
            
        } catch (NumberFormatException e) {
            Logger.getLogger(DeleteMessageActionBean.class.getName()).log(Level.SEVERE, null, e);

        }
    }

    /**
     * Edit message.
     * @return Resolution
     */
    public Resolution submit() {

        /* User is logged */
        if (null != context.getRequest().getSession().getAttribute("user")) {

            /* Get user info from session */
            UserBean user = (UserBean) context.getRequest().getSession().getAttribute("user");
            
            /* User is admin */
            if (user.getAdmin() == 1) {

                Message message = new Message();
                message.setDb(this.context.getServletContext());
                Forum forum = new Forum();
                forum.setDb(this.context.getServletContext());


                try {
                    int idEditValue = Integer.valueOf(idEdit);
                    int idShowValue = Integer.valueOf(idShow);

                    /* Edit message */
                    message.editMessage(idEditValue, text);

                    /* Show forum with edited message */
                    forum.showForumById(idShowValue);
                    
                    return new ForwardResolution("/forum.action?id=" + idShow);

                } catch (NumberFormatException e) {
                    Logger.getLogger(DeleteMessageActionBean.class.getName()).log(Level.SEVERE, null, e);
                    return new ForwardResolution("/index.action");
                }

            }
        }
        /* Some error, show index */
        return new ForwardResolution("/index.action");
    }
}
